Designing the Hybrid Architecture
To adapt this application to ColdFusion, it is important to first understand where ColdFusion elements stand in relation to J2EE elements. At first glance, it seems like ColdFusion pages would simply trade one-for-one with JSP templates. Like JSP templates, CFML pages allow for simple tag-based scripting and easily embedding dynamic programming within an HTML page. CFML pages are also compiled into servlets. However, a number of tools in the ColdFusion application framework also enable ColdFusion to act as the application controller.
ColdFusion to handle Controller and View in the MVC.
For starters, the Application.cfm and OnRequestEnd.cfm
pages automatically execute before and after a browser requests a page
from the server. This allows you to embed security checks, application,
request, and session scope data handling, among other things. In particular,
for this application, it would be extremely easy to integrate the cflogin
and cflogout tags into the application.
It was clear that I would use ColdFusion in the hybrid application to replace the JSP pages and the servlets. This seemed a good idea for a number of reasons. To start, CFML is easier to learn than Java. This means that a company’s designers and web development staff can get learn quickly and there is no necessity to hire expensive programmers or distract any current programmers. If you need to hire additional developers, an experienced ColdFusion programmer will likely cost less than an experienced Java programmer.
J2EE handles business logic and transactions well. However, it seemed that I could use ColdFusion for more than just the front end—or that I could at least explore it for comparison. The fact stands that J2EE is good for handling business logic and transactions. This is true for a number of reasons. Java has a number of APIs that allow quick access to applications, registries and databases of all kinds. This includes the Java Naming Directory Interfact (JNDI), Java DataBase Connectivity (JDBC), and Java Connector Architecture (JCA) APIs. Additionally, J2EE incorporates a messaging service (Java Messaging Service, JMS) that allows you to send data back and forth between applications.
Perhaps most importantly, the EJB specification was designed to empower developers with a scalable and powerful construct to house a data model, handle transactions, and execute business logic. Because an EJB container allows remote deployment, you can deploy EJB objects on their own server, or cluster of servers. If necessary, a single bean (such as the Reservation bean) could be handled by its own cluster of servers. Additionally, entity beans were designed to interact with databases and allow for rolling back transactions and other important functionality that direct transactions don’t have. Message beans were designed to handle messaging using JMS. Session beans were designed to either handle business logic or store session information. In short, the EJB container alone is reason enough to use J2EE for the model aspect of any J2EE or hybrid application.
Another reality is that many enterprise environments already use Java to handle business logic. It only makes sense to incorporate this logic directly into and web-based or enterprise level applications. The Java programmers can continue to develop classes and EJB components, and the web developers and/or designers can access them through a simple and powerful scripting environment: ColdFusion MX.
Using ColdFusion components as façades pattern. One of the new features in the ColdFusion MX release was the ColdFusion component (CFC). With CFCs, ColdFusion developers can now encapsulate custom functions and easily invoke them from within web services. You write CFCs with CFML—either through CFScript or using CFML tags. In many ways, CFCs are like Java classes. Each CFC can hold a number of different functions within it. In pure ColdFusion applications, this is a great way to separate business logic and transaction handling from the UI. Simply create CFCs that handle business logic, and then invoke them from CFML templates when you need them.
When developing a hybrid application, however, it seems most logical to let Java handle all of the business logic and transaction processing; That is what it does best. But using a CFC to abstract calls to EJB components and other Java classes makes sense for of the following reasons:
- Developing prototypes. By encapsulating pseudo-business
logic or transactions in CFCs, a ColdFusion developer can develop
a prototype application and UI without having to worry about major
changes when you eventually call it. For example, a developer could
create a CFC to handle reservations with the function
reserve(). This function return the order number. In the prototype, a CFC would create that simple set and return a number. When the application goes live, this CFC can handle the transaction directly, or make calls to the appropriate Java classes or beans. - Keeping UI and logic separate. One key aspect
of application architecture is differentiating between the user interface
and the business logic. This is often a challenge—especially
in previous versions of ColdFusion (before ColdFusion MX). Before
CFCs, separating these elements required adopting (often complex)
programming methodologies that used the
cfincludetag, user-defined functions, and flow control templates. In the J2EE world, JSP often falls short of this goal too (even though the Java community recommends this technique), with JavaBeans components. To perform most dynamic tasks in a JSP tag, you still had to basically understand the Java language. To ease this problem, the Java community presented a number of alternatives, and still introduce more, such including JavaServer Faces, custom JSP tags, and more. Ultimately, these alternatives become quite complex, even though they are different from traditional JSP. ColdFusion MX, with its mix of CFML templates and CFCs could be a viable alternative.
Overall, by using CFCs as a façade ColdFusion developers can keep the necessary coding, such as invoking Java objects, out of the CFML pages and encapsulated within the CFCs. The additional overhead caused by calling a CFC that calls an object (instead of calling directly on the object) is minimal since CFML pages are ultimately compiled into servlets. Figure 3 illustrates the basic hybrid application architecture:
Figure 3. The hybrid application architecture.
Submit feedback on our tutorials, articles, and sample applications.